﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace DotNETX.Business.Module.MefExtension
{
    public class MefBusinessProcessor : BusinessProcessor
    {
        public override void LoadModules()
        {
            string dir = DotNETX.Utility.Config.AppConfigUtils.TryGetSetting<string>(System.Reflection.Assembly.GetExecutingAssembly(), "DotNETX.Business.Module.MefExtension.ModuleDirectory", "Modules");
            if (!System.IO.Path.IsPathRooted(dir))
            {
                dir = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), dir);
            }
            var catalog = new DirectoryCatalog(dir);
            var container = new CompositionContainer(catalog);
            //将部件（part）和宿主程序添加到组合容器
            container.ComposeParts(this);

            foreach (var m in InternalModuleList)
            {
                m.Initial();
                Modules[m.Name] = m;
            }
        }

        [ImportMany(AllowRecomposition = true)]
        protected List<MefBusinesModule> InternalModuleList { get; set;}
    }
}
